package com.cxrj.zhsq.propertyManage.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.cxrj.zhsq.propertyManage.dao.NoticeMapper;
import com.cxrj.zhsq.propertyManage.dao.NoticeUserMapper;
import com.cxrj.zhsq.propertyManage.entity.Notice;
import com.cxrj.zhsq.propertyManage.entity.NoticeUser;
import com.cxrj.zhsq.propertyManage.service.NoticeSecvice;
import com.cxrj.zhsq.userManage.dao.AppUserMapper;
import com.cxrj.zhsq.userManage.entity.AppUser;
import com.cxrj.zhsq.utils.HtmlUtil;
import com.cxrj.zhsq.utils.IpUtil;
import com.cxrj.zhsq.utils.UploadController;


@Service
public class NoticeServiceImpl implements NoticeSecvice {
	
	@Resource
	private NoticeMapper noticeMapper;
	
	@Resource
	private NoticeUserMapper noticeUserMapper;
	
	@Resource
	private AppUserMapper appUserMapper;
	
	@Override
	public void insertNotice(Notice notice) {
			notice.setCreateTime(new Date());
			notice.setNoticeState("0");
			noticeMapper.insertSelective(notice);
			List<AppUser> list = appUserMapper.selectAllAppUser();
			List<NoticeUser> noticeUsers = new ArrayList<NoticeUser>();
			for (AppUser appUser : list) {
				NoticeUser noticeUser = new NoticeUser();
				noticeUser.setId(UUID.randomUUID().toString());
				noticeUser.setnId(notice.getNoticeId());
				noticeUser.setStatus("0");
				noticeUser.setUserId(appUser.getUserId());
				noticeUsers.add(noticeUser);
			}
			noticeUserMapper.insertByList(noticeUsers);
	}
	
	@Override
	public Map<String, Object> updateNotice(Notice notice) {
		Map<String, Object> resultMap=new HashMap<>();
		try {
			noticeMapper.updateByPrimaryKeySelective(notice);
			resultMap.put("result", "1");
		} catch (Exception e) {
			resultMap.put("result", "-1");
			e.printStackTrace();
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> selectNoticePage(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap=new HashMap<String, Object>();
		Map<String, Object> sqlMap=new HashMap<String, Object>();
		response.setContentType("text/html;charset=utf-8");
		//数据起始位置
		String start=request.getParameter("offset");
		//数据长度
		String length=request.getParameter("limit");
		//查询条件
		String kod=request.getParameter("kod");
		String kodType=request.getParameter("kodType");
		String sqlStr = " 1=1 ";
		
		//总条数
		String recordsTotal = "0";

		if(kod != null && !kod.equals("")){
			sqlStr += "and notice_title like " + '"' + '%' + kod + '%' + '"';
		}
		if(kodType != null && !kodType.equals("")){
			sqlStr += "and notice_type = " + kodType;
		}
		sqlMap.put("condition", "where "+sqlStr);
		sqlMap.put("start", Integer.parseInt(start));
		sqlMap.put("length", Integer.parseInt(length));
		recordsTotal = noticeMapper.getNoticeCount(sqlStr) + "";
		List<Map<String, Object>> noticeList=noticeMapper.selectNoticePage(sqlMap);
		
		resultMap.put("total", recordsTotal);
		resultMap.put("recordsFiltered", recordsTotal);
		resultMap.put("rows", noticeList);
		
		return resultMap;
	}

	@Override
	public Map<String, Object> selectById(String noticeId, String userId, HttpServletRequest request) {
		Map<String, Object> noticeMap=new HashMap<>();
		Notice notice= noticeMapper.selectByPrimaryKey(noticeId);
		if (notice.getNoticeContent() != null && !notice.getNoticeContent().equals("")) {
			notice.setNoticeContent(HtmlUtil.getAbsSource(notice.getNoticeContent(), "http://" + IpUtil.getIpAddr(request) + ":" + request.getServerPort() + "/"));
		}
		if(userId != null) {
			Map<String, Object> sqlMap=new HashMap<String, Object>();
			sqlMap.put("userId", userId);
			sqlMap.put("noticeId", noticeId);
			noticeUserMapper.updateByRead(sqlMap);
		}
		noticeMap.put("notice", notice);
		return noticeMap ;
	}

	@Override
	public Map<String, Object> deleteNotice(String noticeId,HttpServletRequest request,HttpServletResponse response) {
		Map<String, Object> resultMap=new HashMap<>();
		try {
			noticeMapper.deleteByPrimaryKey(noticeId);
			noticeUserMapper.deleteByList(noticeId);
			UploadController.deleteDir(noticeId, request, response);
			resultMap.put("result", "1");
		} catch (Exception e) {
			resultMap.put("result", "-1");
			e.printStackTrace();
		}
		
		return resultMap;
	}

	@Override
	public Map<String, Object> updatereleaseNotice(String noticeId, String noticeState) {
		Map<String, Object> resultMap=new HashMap<String, Object>();
		Notice notice=new Notice();
		notice.setNoticeId(noticeId);
		notice.setNoticeState(noticeState);
		notice.setNoticeTime(new Date());
		int a=noticeMapper.updateByPrimaryKeySelective(notice);
		resultMap.put("result", "-1");
		if (a>0) {
			resultMap.put("result", "1");
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> selectNoticeByTopThree(String userId) {
		Map<String, Object> resultMap=new HashMap<String, Object>();
		Map<String, Object> sqlMap=new HashMap<String, Object>();
		sqlMap.put("userId", userId);
		sqlMap.put("start", 0);
		sqlMap.put("length", 3);
		resultMap.put("list", noticeMapper.selectNoticeUserByPage(sqlMap));
		return resultMap;
	}

	@Override
	public Map<String, Object> selectNoticeByStatus(String userId, String status, String dataIndex, String pageCount) {
		Map<String, Object> resultMap=new HashMap<String, Object>();
		Map<String, Object> sqlMap=new HashMap<String, Object>();
		sqlMap.put("userId", userId);
		sqlMap.put("noticeStatus", status);
		sqlMap.put("start", Integer.parseInt(dataIndex));
		sqlMap.put("length", Integer.parseInt(pageCount));
		resultMap.put("list", noticeMapper.selectNoticeUserByPage(sqlMap));
		return resultMap;
	}

	@Override
	public Map<String, Object> updateNoticeByRead(String userId, String noticeId) {
		Map<String, Object> resultMap=new HashMap<String, Object>();
		Map<String, Object> sqlMap=new HashMap<String, Object>();
		sqlMap.put("userId", userId);
		sqlMap.put("noticeId", noticeId);
		Integer c = noticeUserMapper.updateByRead(sqlMap);
		resultMap.put("result", "0");
		if (c > 0) {
			resultMap.put("result", "1");
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> selectNoticeByNotRead(String userId) {
		Map<String, Object> resultMap=new HashMap<String, Object>();
		Map<String, Object> sqlMap=new HashMap<String, Object>();
		sqlMap.put("userId", userId);
		sqlMap.put("type", "2");
		Integer bmxxCount = noticeUserMapper.selectNoticeByNotRead(sqlMap);
		sqlMap.put("type", "3");
		Integer hdtzCount = noticeUserMapper.selectNoticeByNotRead(sqlMap);
		sqlMap.put("type", "4");
		Integer wytzCount = noticeUserMapper.selectNoticeByNotRead(sqlMap);
		sqlMap.put("type", "5");
		Integer xttzCount = noticeUserMapper.selectNoticeByNotRead(sqlMap);
		resultMap.put("bmxxCount", bmxxCount);
		resultMap.put("hdtzCount", hdtzCount);
		resultMap.put("wytzCount", wytzCount);
		resultMap.put("xttzCount", xttzCount);
		return resultMap;
	}
}
